package encomendaFacil.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import encomendaFacil.bean.Package;
import encomendaFacil.bean.User;
import encomendaFacil.dao.ColumnIndexes;
import encomendaFacil.dao.DAOFactory;
import encomendaFacil.dao.Queries;

public class PackageServiceImpl implements PackageService {

	public Package insert(Map<String, Object> data){
		String trackingCode = (String) data.get(ColumnIndexes.TRACKING_CODE);
		String name = (String) data.get(ColumnIndexes.PACKAGE_NAME);
		Long ownerId = (Long) data.get(ColumnIndexes.OWNER_ID);
		String status = (String) data.get(ColumnIndexes.STATUS);
		User owner = DAOFactory.getInstance().getUserDAO().findById(ownerId);
		
		Package pack = new Package(0L, trackingCode, name, new Date(), new Date(), owner, status, false);
		pack = DAOFactory.getInstance().getPackageDAO().insert(pack);
		
		return pack;
	}
	
	public Package modify(Map<String, Object> data){
		String trackingCode = (String) data.get(ColumnIndexes.TRACKING_CODE);
		String name = (String) data.get(ColumnIndexes.PACKAGE_NAME);
		Long ownerId = (Long) data.get(ColumnIndexes.OWNER_ID);
		User owner = DAOFactory.getInstance().getUserDAO().findById(ownerId);
		Long id = (Long) data.get(ColumnIndexes.PACKAGE_ID);
		
		Package pack = new Package();
		pack.setTrackingCode(trackingCode);
		pack.setName(name);
		pack.setOwner(owner);
		pack.setId(id);
		pack = DAOFactory.getInstance().getPackageDAO().insert(pack);
		
		return pack;
		
	}
	
	public void remove(Long id){
		DAOFactory.getInstance().getPackageDAO().remove(id);
	}
	
	public boolean updateDelivered(Map<String, Object> data){
		boolean isDelivered = (boolean) data.get(ColumnIndexes.IS_DELIVERED);
		Long id = (Long) data.get(ColumnIndexes.PACKAGE_ID);
		Long ownerId = (Long) data.get(ColumnIndexes.OWNER_ID);
		User owner = DAOFactory.getInstance().getUserDAO().findById(ownerId);
		
		Package pack = new Package();
		pack.setDelivered(isDelivered);
		pack.setOwner(owner);
		pack.setId(id);
		pack.setLastModification(new Date());
		boolean altered = DAOFactory.getInstance().getPackageDAO().updateDelivered(pack);
		
		return altered;
		
	}
	
	public boolean updateStatus(Map<String, Object> data){
		String status = (String) data.get(ColumnIndexes.STATUS);
		Long id = (Long) data.get(ColumnIndexes.PACKAGE_ID);
		Long ownerId = (Long) data.get(ColumnIndexes.OWNER_ID);
		User owner = DAOFactory.getInstance().getUserDAO().findById(ownerId);
		
		Package pack = new Package();
		pack.setStatus(status);
		pack.setId(id);
		pack.setOwner(owner);
		pack.setLastModification(new Date());
		
		boolean altered = DAOFactory.getInstance().getPackageDAO().updateStatus(pack);
		
		return altered;
	}

	@Override
	public Package findOne(Map<String, Object> params) {
		
		String initialQuery = Queries.FIND_PACKAGE;
		
		StringBuffer query = new StringBuffer();
		query.append(initialQuery);
		query.append(" WHERE ");
		
		int count = 0;
		for (Entry<String, Object> param: params.entrySet()) {
			String key = param.getKey();
			Object value = param.getValue();
			
			query.append(key);
			
			if (key.equals("name")) {
				query.append(" LIKE ");
			}
			else{
				query.append("=");
			}
			
			if (value instanceof Long) {
				query.append((Long) value);
			}
			else{
				query.append(value);
			}
			
			count++;
			
			if (count == params.size()) {
				break;
			}
			else{
				query.append(" AND ");
			}
			
		}
		
		return DAOFactory.getInstance().getPackageDAO().findOne(query.toString());
	}

	@Override
	public List<Package> find(Map<String, Object> params) {
		
		String initialQuery = Queries.FIND_PACKAGE;
		StringBuffer query = new StringBuffer();
		query.append(initialQuery);
		
		int quantParams = params.size();
		if (quantParams == 0) {
			return DAOFactory.getInstance().getPackageDAO().find(query.toString());
		}
		
		query.append(" WHERE ");
		
		int count = 0;
		for (Entry<String, Object> param: params.entrySet()) {
			String key = param.getKey();
			Object value = param.getValue();
			
			query.append(key);
			
			if (key.equals("name")) {
				query.append(" LIKE ");
			}
			else{
				query.append("=");
			}
			
			if (value instanceof Long) {
				query.append((Long) value);
			}
			else{
				query.append(value);
			}
			
			count++;
			
			if (count == params.size()) {
				break;
			}
			else{
				query.append(" AND ");
			}
			
		}
		
		return DAOFactory.getInstance().getPackageDAO().find(query.toString());
	}
	
}
